package com.bst.etl.datax.web;

import com.alibaba.druid.pool.DruidDataSource;
import com.bst.common.constant.JobConstant;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.sql.EtlUtil;
import com.bst.datatx.core.biz.model.JobParam;
import com.bst.datatx.core.biz.model.ReturnT;
import com.bst.datatx.core.util.DateUtil;
import com.bst.etl.datax.core.conf.JobAdminConfig;
import com.bst.etl.datax.core.cron.CronExpression;
import com.bst.etl.datax.core.thread.JobTriggerPoolHelper;
import com.bst.etl.datax.core.trigger.TriggerTypeEnum;
import com.bst.etl.datax.core.util.I18nUtil;
import com.bst.etl.datax.dto.TriggerJobDto;
import com.bst.etl.datax.entity.JobLog;
import com.bst.etl.datax.mapper.JobLogMapper;
import com.bst.etl.datax.service.JobTemplateService;
import com.bst.etl.domain.EtlDrawBaseDatax;
import com.bst.etl.domain.EtlDrawDm;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.system.framework.datasource.DynamicDataSource;
import com.bst.system.service.ISysJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * index controller
 *
 * @author xuxueli 2015-12-19 16:13:16
 */
@Api(tags = "任务配置接口")
@Controller
@RequestMapping("/base/job/trigger")
public class JobTriggerController extends BaseController {
    @Resource
    private JobLogMapper jobLogMapper;
    @Autowired
    JobTemplateService jobTemplateService;

    @PostMapping(value = "")
    @ApiOperation("触发任务")
    @ResponseBody
    public AjaxResult triggerJob(@RequestBody TriggerJobDto dto) {
        // force cover job param
        jobTemplateService.jobTemplateTrigger(dto, TriggerTypeEnum.MANUAL);
        //JobTriggerPoolHelper.trigger();
        return AjaxResult.success();
    }



    @GetMapping("/nextTriggerTime")
    @ApiOperation("获取近5次触发时间")
    public ReturnT<List<String>> nextTriggerTime(String cron) {
        List<String> result = new ArrayList<>();
        try {
            CronExpression cronExpression = new CronExpression(cron);
            Date lastTime = new Date();
            for (int i = 0; i < 5; i++) {
                lastTime = cronExpression.getNextValidTimeAfter(lastTime);
                if (lastTime != null) {
                    result.add(DateUtil.formatDateTime(lastTime));
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
        }
        return new ReturnT<>(result);
    }

}
